<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>学生列表</title>
    <script src="./lib/vue/vue.js"></script>
    <link rel="stylesheet" href="./lib/bootstrap/css/bootstrap.css"/> 
</head>

<body>
    <div id="app">
        <h1 align="center">学生列表</h1><hr>
        <span></span>
        <div class="col-lg-2">
            <input type="text" placeholder="请输入班级" v-model="student.classname">
        </div>
        <div class="col-lg-2">
            <input type="text" placeholder="请输入姓名" v-model="student.name">
        </div>
        <div class="col-lg-2">
            <input type="text" placeholder="请输入性别" v-model="student.sex">
        </div> 
        <div class="col-lg-2">
            <input type="text" placeholder="请输入年龄" v-model="student.age">
        </div>
        <button class="btn btn-primary" v-on:click="add">增加</button>
        <button class="btn btn-primary" v-on:click="del">删除</button>
        <table class="table table-striped">
            <tr>
                <td>班级</td>
                <td>姓名</td>
                <td>性别</td>
                <td>年龄</td>
            </tr>
            <tr v-for="(stu,key) in students">
                <td>{{stu.classname}}</td>
                <td>{{stu.name}}</td>
                <td>{{stu.sex}}</td>
                <td>{{stu.age}}</td>
            </tr>
        </table>
    </div>
    <script>
        var vm=new Vue({
           el:'#app',
           data:{
               students:[
                {classname:"学通班",name:"士兵76",sex:"男",age:56},
                {classname:"智达班",name:"安娜",sex:"女",age:59},
                {classname:"新创班",name:"裂空",sex:"男",age:20},
                {classname:"学林班",name:"末日铁拳",sex:"男",age:32}
               ],
               student:{classname:"终极一班",name:"裂空座",sex:"未知",age:0000},
               
           },
           methods:{
               add(){
                    var stu={};
                    stu.classname = this.student.classname;
                    stu.name = this.student.name;
                    stu.sex = this.student.sex;
                    stu.age = this.student.age;
                    if(stu.classname!='' && stu.name!=''){
                        this.students.push(stu);
                    }else{
                        alert("班级和姓名不能为空");
                        return;
                    }
                    this.student.classname='';
                    this.student.name='';
                    this.student.sex='';
                    this.student.age='';
               },
               del(){
                   this.students.pop();  
               },
               selectAllStudent(){
                var stus = document.getElementsByName("students")
                var stuArr = Array.prototype.slice.call(stus);
                var currentObject = window.event.srcElement;
                    if(currentObject.checked){
                        stuArr.forEach((v) => {
                          v.checked=true;  
                        })
                    }else{
                        stuArr.forEach((v)=>{
                            v.checked=false;
                        })
                    }
               }
           }
        });
    </script>
</body>

</html>